﻿//加载图片面板
function btnTuPian() {
    showTool();
    var tools = getTools();

    if ($(".adimages") && $(".adimages").length > 0)
        $(".adimages").remove();

    var html = "";

    html += "<div class='d_z adtitle clearfix'><div class='adfl dw45'>插入图片(点击插入)</div><div class='adfr adfwn dw45 adtar'><span class='d_z adbtn' data-f='webzifanhui()'>返回</span></div></div><div class='d_z adimages clearfix'></div><div class='adnexturl' >向下滑动加载更多...</div>";

    html += "<span class='d_z adbtn file'>上传<input type=\"file\" /></span></br>";
    html += "<div class='d_z adcontent'>一次请上传一张图片</div>";

    tools.html(html);

    loadTuPian(1);
    bindButtonClickEvent();
}

function loadTuPian(page) {
    getNextUrl().html("</br>正在加载图片中...</br>");
    $.get("/getuploadimg?page=" + page, function (ret) {
        if (ret.indexOf("ok,") != -1) {
            getNextUrl().remove();
            var arr = ret.split(',');
            for (i = 1; i < arr.length - 1; i++) {
                addTupianPan(arr[i]);
            }
            if(arr[arr.length - 1] == "ok")
                getAdImages().after("<div class='adnexturl' data-f='loadTuPian(" + (page + 1) + ")' >向下滑动加载更多图片...</div>");
        }
        else
        {
            if (page == 1)
                getAdImages().html("您还没有上传任何图片!");
            getNextUrl().html("没有数据...");
        }

        bindButtonClickEvent();
    });
}


//把图片加入到层中
function addTupianPan(url) {
    getAdImages().append('<div class="d_z adimgdiv clearfix"><img class="adimg adbtnX adfl" data-f="tupianshowbtn(event.target,\'' + url + '\')" src="' + url + '"/></div>');


    //设置大小
    $(".adimgdiv").width( $(".adimgdiv").parent().width()/2 - 6 );
}


function tupianshowbtn(ths, url) {
    if (getTuPianBtn().length > 0)
        getTuPianBtn().remove();

    var div = $("<div class='adfwn adtar adtupianshowbtn d_z'><span class='d_z adbtn adbgcred' data-f='tupianqueding(\"" + url.replace("_200.", ".") + "\")'>选择</span><span class='d_z adbtn' data-f='tupianshanchu(\"" + url + "\")'>删除</span></div>");

    getAdImages().append(div);

    G_TUPIANELEMENT = $(ths);

    var w = G_TUPIANELEMENT.width();
    var h = G_TUPIANELEMENT.height();
    var x = G_TUPIANELEMENT.position().left;
    var y = G_TUPIANELEMENT.position().top;

    $(".adb1red").removeClass("adb1red");
    G_TUPIANELEMENT.parent().addClass("adb1red");

    getTuPianBtn().css({ left: x + (w - $(".adtupianshowbtn").outerWidth(true))/2, top: y+(h - $(".adtupianshowbtn").outerHeight(true))/2 });
    getTuPianBtn().show();

    bindButtonClickEvent();
}

function tupianshanchu(url) {
    if (getTuPianBtn().length > 0)
        getTuPianBtn().remove();

    //移除这个对象
    //event.target.remove();
    if (G_TUPIANELEMENT && G_TUPIANELEMENT.length > 0)
        G_TUPIANELEMENT.parent().remove();

    $.get("/deleteuploadimg?purl=" + escape(url), function (ret) {

    });
}

function tupianqueding(url) {
    hideMenu();

    var span = $("<div><img class='adimg100' src='" + url + "'></div>");
    //插入内容
    G_CLICKELEMENT.before(span);
    bindElementClickEvent(span, elementClickEvent);
}

